from sys import argv
from sys import exit
import argparse
from cnxiqp.videodownloader.downloader import Downloader
from cnxiqp.videodownloader.youkuflvc import YoukuFlvc

def main():
    parse = argparse.ArgumentParser()
    parse.add_argument('-c', action='store_true', default=False,
        dest='downnext', help='continue to download next url')
    parse.add_argument('-u', action = 'store', dest = 'url', help='the video url')
    args = parse.parse_args()
    print args.downnext
    print args.url
    
    if (args.url == None):
        parse.print_help();
        exit()

    
    url = args.url

    while url:
        youku = YoukuFlvc(url)
        downloader = Downloader(youku.filename, youku.segurls)
        downloader.download()
        if not youku.ntitle:
            print 'There is no next video, exit.'
            url = None
            continue
        
        if args.downnext:
            url = youku.nurl
            continue
        
        ans = 'x'
        while ans not in ['Y','y','N','n']:
            print '========================================================================'
            print 'The next video is', youku.ntitle, ', Download it [y/n]:'
            ans = raw_input()
            
        if ans == 'N' or ans == 'n':
            url = None
        else:
            url = youku.nurl
    
if __name__ == '__main__':
    main()